using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using OrderingSystem.Core.Entities;
using OrderingSystem.Core.Interfaces;
using System.Security.Claims;

namespace OrderingSystem.API.Controllers
{
    [ApiController]
    [Route("storeup")]
    public class FavoriteController : ControllerBase
    {
        private readonly IFavoriteService _favoriteService;

        public FavoriteController(IFavoriteService favoriteService)
        {
            _favoriteService = favoriteService;
        }

        /// <summary>
        /// 获取收藏列表
        /// </summary>
        [HttpGet("list")]
        [Authorize]
        public async Task<ActionResult<R>> GetUserFavorites()
        {
            var userId = long.Parse(User.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? "0");
            var favorites = await _favoriteService.GetUserFavoritesAsync(userId);
            return Ok(new R
            {
                code = 0,
                msg = "获取成功",
                data = favorites
            });
        }

        /// <summary>
        /// 获取收藏详情
        /// </summary>
        [HttpGet("detail/{id}")]
        [Authorize]
        public async Task<ActionResult<R>> GetFavorite(long id)
        {
            var favorite = await _favoriteService.GetFavoriteAsync(id);
            if (favorite == null)
            {
                return Ok(new R { code = 1, msg = "收藏不存在" });
            }

            var userId = long.Parse(User.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? "0");
            if (favorite.userid != userId)
            {
                return Ok(new R { code = 1, msg = "无权查看此收藏" });
            }

            return Ok(new R
            {
                code = 0,
                msg = "获取成功",
                data = favorite
            });
        }

        /// <summary>
        /// 添加收藏
        /// </summary>
        [HttpPost("save")]
        [Authorize]
        public async Task<ActionResult<R>> CreateFavorite([FromBody] Favorite favorite)
        {
            var userId = long.Parse(User.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? "0");
            favorite.userid = userId;

            var result = await _favoriteService.CreateFavoriteAsync(favorite);
            if (result == null)
            {
                return Ok(new R { code = 1, msg = "已经收藏过了" });
            }

            return Ok(new R
            {
                code = 0,
                msg = "收藏成功",
                data = result
            });
        }

        /// <summary>
        /// 删除收藏
        /// </summary>
        [HttpPost("delete/{id}")]
        [Authorize]
        public async Task<ActionResult<R>> DeleteFavorite(long id)
        {
            var favorite = await _favoriteService.GetFavoriteAsync(id);
            if (favorite == null)
            {
                return Ok(new R { code = 1, msg = "收藏不存在" });
            }

            var userId = long.Parse(User.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? "0");
            if (favorite.userid != userId)
            {
                return Ok(new R { code = 1, msg = "无权删除此收藏" });
            }

            await _favoriteService.DeleteFavoriteAsync(id);
            return Ok(new R { code = 0, msg = "删除成功" });
        }

        /// <summary>
        /// 检查是否已收藏
        /// </summary>
        [HttpGet("check")]
        [Authorize]
        public async Task<ActionResult<R>> CheckFavorite([FromQuery] long refId, [FromQuery] string type)
        {
            var userId = long.Parse(User.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? "0");
            var isFavorited = await _favoriteService.IsFavoritedAsync(userId, refId, type);

            return Ok(new R
            {
                code = 0,
                msg = "获取成功",
                data = isFavorited
            });
        }
    }
} 